package ui;

import java.awt.Button;
import java.awt.Rectangle;
import java.awt.TextArea;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import constants.CurrentConfigConstants;
import core.SendMsg;

public class MainWindow extends JPanel {

	UIManager foo = null;
	
	public MainWindow(UIManager foo) {
		
		this.foo = foo;
		
		setControl();
		setContentPanel();
		setListener();
	}
	
	///
	/// 控件
	///
	TextArea textArea_Msg = new TextArea();  		// 接收到的消息
	TextArea textArea_Send = new TextArea(); 		// 待发送的消息
	Button btn_send = new Button(); 				// 发送消息 按钮
	Button btn_preference = new Button();			// 偏好设置
	Button btn_close = new Button();
	JLabel lb_ip = new JLabel();
	JLabel lb_localIP = new JLabel();				// 显示本机地址
	JComboBox cb_ip = new JComboBox();				// 发送目的地 下拉框
	
	/**
	 * 设置控件
	 * @throws Exception
	 */
	private void setControl() {
		
		textArea_Msg.setColumns(0);
		textArea_Msg.setRows(0);
		textArea_Msg.setEditable(false);
		textArea_Msg.setSelectionStart(10);
		textArea_Msg.setSelectionEnd(0);
		textArea_Msg.setText("NinTalk\n");
		
		textArea_Send.setColumns(10);
		textArea_Send.setText("");
		
		btn_send.setLabel("Send");
		btn_preference.setLabel("Preference");
		btn_close.setLabel("Close");
		
		lb_ip.setText("IP address:");
		
		lb_localIP.setText("localIP: " + new util.Local().getLocalIP());
		
		cb_ip.setEditable(true);
		cb_ip.addItem("127.0.0.1");
		cb_ip.setToolTipText("这里输入IP");
		
	}
	
	/**
	 * 设置主面板
	 * @throws Exception
	 */
	private void setContentPanel() {
		
		
		// 添加控件到面板
		this.add(textArea_Msg, null);
		this.add(textArea_Msg, null);
		this.add(textArea_Send, null);
		this.add(lb_ip, null);
		this.add(cb_ip, null);
		this.add(btn_send, null);
		this.add(btn_preference, null);
		this.add(btn_close, null);
		this.add(lb_localIP, null);
		
		// 布局控制
		// X, Y , Width, Height
		this.setLayout(null);
		this.setBounds(5, 5, 410, 410);
		textArea_Msg.setBounds(new Rectangle(5, 5, 390, 135));
		textArea_Send.setBounds(new Rectangle(5, 150, 390, 135));
		lb_ip.setBounds(new Rectangle(5, 300, 70, 20));
		cb_ip.setBounds(new Rectangle(80, 300, 100, 20));
		btn_send.setBounds(new Rectangle(310, 300, 80, 30));
		btn_preference.setBounds(new Rectangle(310, 350, 80, 30));
		btn_close.setBounds(231,350,80,30);
		lb_localIP.setBounds(new Rectangle(5, 350, 200, 20));
	}
	
	/**
	 * 设置监听器
	 * @throws Exception
	 */

	private void setListener() {
		// btn_preference 点击事件
		btn_preference.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				foo.mainWindow.setVisible(false);
				foo.configWindow.setVisible(true);
			}
		});
		// btn_close 点击事件
		btn_close.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				System.exit(0);
			}
		});
		// btn_send 点击事件
		btn_send.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				dto.Message msg = new dto.Message();
				util.Local local = new util.Local();
				SendMsg sendMsg = new SendMsg();
				
				msg.setContent(textArea_Send.getText());
				msg.setFromIP(local.getLocalIP());
				msg.setToIP(cb_ip.getSelectedItem().toString().trim());
				msg.setFromName(CurrentConfigConstants.CLIENT_CURRENT_NAME);
				
				textArea_Send.setText("");
				
				sendMsg.setMsg(msg);
				if (sendMsg.send())
					textArea_Msg.append("[Message Send: " + msg.getToIP() + "]\n");
			}
		});
		
	}
}
